const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
  
    // 背景图片
    backgroundImage: 'https://scoringchina.oss-cn-beijing.aliyuncs.com/xijiuclassicopen/statics/images/2025/liucheng_bg.jpg',
    // 六个城市的数据
    cityList: [
      {
        id: 1,
        name: '区域赛 - 北京站',
        date: '9月1日',
        image: 'https://scoringchina.oss-cn-beijing.aliyuncs.com/xijiuclassicopen/statics/images/2025/liucheng_beijing.png',
        liveUrl: '/pages/liveroom/index?city=beijing',
      },
      {
        id: 2,
        name: '区域赛 - 青岛站',
        date: '9月12日',
        image: 'https://scoringchina.oss-cn-beijing.aliyuncs.com/xijiuclassicopen/statics/images/2025/liucheng_qingdao.png',
        liveUrl: '/pages/liveroom/index?city=qingdao',
      },
      {
        id: 3,
        name: '区域赛 - 郑州站',
        date: '9月12日',
        image: 'https://scoringchina.oss-cn-beijing.aliyuncs.com/xijiuclassicopen/statics/images/2025/liucheng_zhengzhou.png',
        liveUrl: '/pages/liveroom/index?city=zhengzhou',
      },
      {
        id: 4,
        name: '区域赛 - 长沙站',
        date: '9月12日',
        image: 'https://scoringchina.oss-cn-beijing.aliyuncs.com/xijiuclassicopen/statics/images/2025/liucheng_changsha.png',
        liveUrl: '/pages/liveroom/index?city=changsha',
      },
      {
        id: 5,
        name: '区域赛 - 清远站',
        date: '9月7日',
        image: 'https://scoringchina.oss-cn-beijing.aliyuncs.com/xijiuclassicopen/statics/images/2025/liucheng_qingyuan.png',
        liveUrl: '/pages/liveroom/index?city=qingyuan',
      },
      {
        id: 6,
        name: '区域赛 - 深圳站',
        date: '9月12日',
        image: 'https://scoringchina.oss-cn-beijing.aliyuncs.com/xijiuclassicopen/statics/images/2025/liucheng_shenz.png',
        liveUrl: '/pages/liveroom/index?city=shenzhen',
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getCityData()
  },

  /**
   * 获取城市数据
   */
  getCityData() {
  let that = this;
  wx.showLoading({ title: '加载中...' });
  wx.request({
    url: 'https://mapp.scoringchina.com/interface/xijiuopen/vote/zhiboindex',
    method: 'GET',
    data: {
 
    },
    success: (res) => {
      console.log('-----------------获取城市数据-------------------',res.data.data);
      if (res.data && res.data.data) {
        let newData = res.data.data;
        that.setData({ backgroundImage: newData.bg_img });
        // cityList 分别取值  key 并一个一个添加数组  beijing  qingdao  zhengzhou  changsha  qingyuan  shenzhen
        let updatedCityList = that.data.cityList.map(city => {
          switch(city.liveUrl.split('city=')[1]) {
            case 'beijing':
              return { ...city, ...newData.beijing };
            case 'qingdao':
              return { ...city, ...newData.qingdao };
            case 'zhengzhou':
              return { ...city,...newData.zhengzhou };
            case 'changsha':
              return { ...city, ...newData.changsha };
            case 'qingyuan':
              return { ...city, ...newData.qingyuan };
            case 'shenzhen':
              return { ...city, ...newData.shenzhen };
            default:
              return city;
          }
        });
        that.setData({ cityList: updatedCityList });
        console.log(that.data.cityList);
     
      } else {
        wx.showToast({ title: '数据格式异常', icon: 'none' });
      }
    },
    fail: () => {
      wx.showToast({ title: '加载失败', icon: 'none' });
    },
    complete: () => {
      wx.hideLoading();
    }
  });
},

  /**
   * 城市卡片点击事件
   */
  onCityTap(e) {
    const city = e.currentTarget.dataset.city
    const index = e.currentTarget.dataset.index
    console.log('点击城市:', city)
    
    // 可以跳转到城市详情页面
      wx.navigateTo({
           url: '/pages/URL/index?title='+city.name+'&weburl='+encodeURIComponent(city.url),
         })
  },

  /**
   * 进入直播按钮点击事件
   */
  onLiveTap(e) {
    e.stopPropagation() // 阻止事件冒泡
    const url = e.currentTarget.dataset.url
    const city = e.currentTarget.dataset.city
    
    console.log('进入直播:', city.name)
    
    wx.navigateTo({
      url: url
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {

    }
  },

  /**
   * 用户点击右上角分享到朋友圈
   */
  onShareTimeline: function () {
    return {
  
    }
  }
})